package states;

import ect.GeneralPause;
import globals.DataTracker;
import globals.Vector2D;
import interfaces.Controller;
import interfaces.StateController;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import pong.*;

public class Pong extends BasicGameState {
	//control
	StateController sController;
	public Input input; // key imput
	GeneralPause gPause;
	
	Controller pad1_Controller,pad2_Controller;
	static public int cVc=0,pVc=1,pVp=2; // comVcom=0, playerVcom=1, playerVplayer=2
	static public int state=cVc;
	
	//Objects
	Ball ball0;
	Paddle pad1,pad2;
	Paddle pad11,pad22;
	
	int gTimer=0, gTimerMax=SlickGame.fps*10;
	
	private int id;
	Pong(int stateId){
		//control
		this.id=stateId;
		sController=new StateController();
		sController.setDelay(3*SlickGame.fps);
		gPause=new GeneralPause("Pong",id);
		gPause.isPaused=true;
		
		pad1_Controller=new Controller();
		pad2_Controller=new Controller();
	
		ball0=new Ball(SlickGame.width/2,SlickGame.height/2,6,true);
		ball0.direction.setMagnatude(12);
		/******/
		pad1=new Paddle((int) (SlickGame.width*(0.2)),SlickGame.height/2,false);
		pad2=new Paddle((int) (SlickGame.width*(0.8)),SlickGame.height/2,true);
	}
	
	public void resetRoom(){
		ball0=new Ball(SlickGame.width/2,SlickGame.height/2,6,true);
		pad1=new Paddle((int) (SlickGame.width*(0.2)),SlickGame.height/2,false);
		pad2=new Paddle((int) (SlickGame.width*(0.8)),SlickGame.height/2,true);
	}
	
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		gPause.init(gc, sbg);
	}

	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		g.setBackground(Color.black);
		g.setColor(Color.white);
		
		pad1.draw(g);
		pad2.draw(g);

		ball0.draw(g);
		
		g.setColor(Color.white);
		g.drawString("Ball0: oor?:"+ball0.outOfRoom, SlickGame.width/2, SlickGame.height/2);
		
		g.setColor(Color.cyan);
		if(gTimer>0){
			g.drawString("GOAL!!!",(float)(SlickGame.width*(0.5)),(float)(SlickGame.height*(0.6)));
		}
		
		
		g.setColor(Color.gray.darker());
		g.drawString("Ball 0 - Collision Type 0 - - Natural\nBall 1 - Collision Type 1 - - Wall Stronger\nBall2 - Collision Type 2 - - Pong Standard", 70, 100);
		g.drawString("Blank",SlickGame.width/2-20,20);
		g.drawString("Mouse: ["+Mouse.getX()+" "+(SlickGame.height-Mouse.getY())+"] ("+Mouse.isButtonDown(0)+")", SlickGame.width/2-75, SlickGame.height-25);
		
		if(gPause.isPaused){
			gPause.draw(g);
		}
	}

	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		this.input = gc.getInput();
		sController.update(input, delta);
		
		if(ball0.outOfRoom && gTimer<=0){gTimer=gTimerMax;}
		else if(gTimer>0){gTimer-=delta;}
		
		if(sController.key_1){
			sController.key_1=false;
			ball0=new Ball(SlickGame.width/2,SlickGame.height/2,6,true);
		}
		if(sController.key_2){
			sController.key_2=false;
		}
		if(sController.key_lShift){
			sController.key_lShift=false;
			gPause.pauseState=gPause.playState;
			gPause.togglePause();
		}
		
		// left off here!
		//pad1.isUp=true;
		
		if(!gPause.isPaused){
			pad1_Controller.specialTrack(pad1, ball0, (float) 1);
			pad2_Controller.updateAutoTrack(pad2, ball0, (float) 2);
			
			pad1.update(delta);
			pad2.update(delta);
			
			ball0.update(delta);
			
			pad1.checkCollision(ball0);
			pad2.checkCollision(ball0);
		}
		else{
			gPause.update(sbg, delta);
		}
	}
	
	public static void destroy(){
		
	}

	public int getID() {
		return this.id;
	}


}
